package 第4周_动态数据结构基础_链表;

public class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int x){
        val = x;
    }

    /**
     * 使用arr为参数创建一个链表，当前的ListNode为链表头结点
     * @param arr
     */
   public ListNode(int[] arr){
       if (arr == null || arr.length == 0){
           throw new IllegalArgumentException("数组为空");
       }

       this.val = arr[0];
       ListNode cur = this;
       for (int i = 0; i < arr.length; i++) {
           cur.next = new ListNode(arr[i]);
           cur = cur.next;
       }
   }

    @Override
    public String toString() {
       StringBuilder res =  new StringBuilder();
       ListNode cur = this;
       while (cur != null){
           res.append(cur.val + "-->");
           cur = cur.next;
       }
       res.append("NULL");
        return res.toString();
    }
}
